package com.softer.wxzj.service.impl;
import com.softer.wxzj.mapper.RoleMapper;
import org.junit.Test;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.MessageEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.entity.FlowInstance;
import com.softer.wxzj.mapper.FlowConfigMapper;
import com.softer.wxzj.mapper.FlowInstanceMapper;
import com.softer.wxzj.service.FlowInstanceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
public class FlowInstanceServiceImpl implements FlowInstanceService {

    @Autowired
    private FlowConfigMapper flowConfigMapper;
    @Autowired
    private static FlowInstanceMapper flowInstanceMapper;

    @Override
    public CommonResult insert(FlowInstance flowInstance) {
        flowInstance.setFlowStep(1);
        //查询当前角色和初始角色
        String code = flowConfigMapper.getRoleCodeByTypeAndStep(flowInstance.getFlowType(), "1");
        flowInstance.setCurrentRoleCode(code);
        flowInstance.setStartRoleCode(code);
        int i = flowInstanceMapper.insert(flowInstance);
        return CommonResult.ResponseResult(i);
    }

    @Override
    public CommonResult update(FlowInstance flowInstance) {
        //判断是做审批还是回退   回退-  审批+
        //根据流程类型和业务id查询步数
        String nextRole = flowInstanceMapper.getNextRoleCodeByTypeAndStep(flowInstance.getBusinessId(), flowInstance.getFlowType());
        if(StringUtils.isBlank(nextRole)){
            //修改业务表
        }else{
            //继续审核
        }
        //新增明细表
        return new CommonResult(ShiroResultEnum.SUCCESS.getCode(), MessageEnum.SUCCESS.getMsg());
    }

    @Override
    public CommonResult delete(String id) {
        return null;
    }

    @Override
    public PageInfo<FlowInstance> getList(Page page, FlowInstance flowInstance) {
        return null;
    }

}
